package com.example.advancecomponents.fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.advancecomponents.R;
import com.example.advancecomponents.vo.Star;

public class StarDetailFragment extends Fragment {

    public static final String NAME = "name";
    public static final String ICON = "icon";
    public static final String DETAIL = "detail";
    public static final String POSITION = "position";


    public static Fragment newInstance(Star star, int position) {
        StarDetailFragment result = new StarDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString(NAME, star.getName());
        bundle.putInt(ICON, star.getIcon());
        bundle.putString(DETAIL, star.getDetail());
        bundle.putInt(POSITION,position);
        result.setArguments(bundle);
        return result;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View result = inflater.inflate(R.layout.star_page_pager, container, false);

        ImageView img = result.findViewById(R.id.img);
        TextView description = result.findViewById(R.id.detailDescription);
        View button = result.findViewById(R.id.goTo);
        button.setVisibility(View.GONE);
        Bundle arguments = getArguments();
        if (arguments != null) {

            int icon = arguments.getInt(ICON, R.drawable.ic_launcher_background);
            img.setImageResource(icon);
            description.setText(arguments.getString(DETAIL));

        }

        result.setOnClickListener(view->{

            Toast.makeText(getContext(), "StarDetailFragment "+getPosition()+"被点击啦", Toast.LENGTH_SHORT).show();

        });

        return result;
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("StarDetailFragment","第 "+getPosition()+" 个Fragment处于 Start 状态");

    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("StarDetailFragment","第 "+ getPosition() +" 个Fragment处于 Resume 状态");
    }

    private int getPosition() {
        return getArguments().getInt(POSITION);
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("StarDetailFragment","第 "+getPosition()+" 个Fragment处于 Pause 状态");

    }
}
